//
//  WDJMainUserView.m
//  wdj
//
//  Created by 蓝鳍互娱 on 2024/7/8.
//

#import "WDJMainUserView.h"
@interface WDJMainUserView()
@property (nonatomic,strong) UILabel *mainUserTitleLab;
@property (nonatomic,strong) UILabel *mainUserVipLab;
@property (nonatomic,strong) UIButton *mainUserOpenVipBtn;
@end
@implementation WDJMainUserView
-(instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = UIColor.clearColor;
        [self wdjCreatView];
    }
    return self;
}
- (void)wdjCreatView{
    UIImageView * mainUserBgImgView = [[UIImageView alloc] init];
    mainUserBgImgView.image = [WDjWholeData wholeImageNamed:@"wdj_MainCenterBg"];
    mainUserBgImgView.userInteractionEnabled = true;
    [self addSubview:mainUserBgImgView];
    [mainUserBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(0));
    }];
    UIImageView * mainUserImgView = [[UIImageView alloc] init];
    mainUserImgView.image = [WDjWholeData wholeImageNamed:@"wdj_MainCenterVip"];
    mainUserImgView.userInteractionEnabled = true;
    [self addSubview:mainUserImgView];
    [mainUserImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:39]));
        make.top.equalTo(@([WDjWholeData wholeScreewRatio:53]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:50]));
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:46]));
    }];
    self.mainUserTitleLab = [[UILabel alloc] init];
    self.mainUserTitleLab.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:38] weight:UIFontWeightRegular];
    self.mainUserTitleLab.textColor = [WDjWholeData wholeColorHexString:@"#855418"];
    [self addSubview:self.mainUserTitleLab];
    [self.mainUserTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(mainUserImgView);
        make.left.equalTo(mainUserImgView.mas_right).offset([WDjWholeData wholeScreewRatio:9]);
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:50]));
    }];
    self.mainUserVipLab = [[UILabel alloc] init];
    self.mainUserVipLab.font = [UIFont systemFontOfSize:[WDjWholeData wholeScreewRatio:24] weight:UIFontWeightRegular];
    self.mainUserVipLab.textColor = [WDjWholeData wholeColorHexString:@"#855418"];
    [self addSubview:self.mainUserVipLab];
    [self.mainUserVipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([WDjWholeData wholeScreewRatio:31]));
        make.top.equalTo(self.mainUserTitleLab.mas_bottom).offset([WDjWholeData wholeScreewRatio:14]);
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:34]));
    }];
    self.mainUserOpenVipBtn = [[UIButton alloc] init];
    [self.mainUserOpenVipBtn setBackgroundImage:[WDjWholeData wholeImageNamed:@"wdj_MainCenterOpenVip"] forState:UIControlStateNormal];
    self.mainUserOpenVipBtn.hidden = true;
    [self.mainUserOpenVipBtn addTarget:self action:@selector(mainUserOpenVip) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.mainUserOpenVipBtn];
    [self.mainUserOpenVipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-[WDjWholeData wholeScreewRatio:47]));
        make.height.equalTo(@([WDjWholeData wholeScreewRatio:62]));
        make.width.equalTo(@([WDjWholeData wholeScreewRatio:180]));
        make.centerY.equalTo(self);
    }];
}
- (void)mainUserOpenVip{
    if(self.MainOpenVipBlock){
        self.MainOpenVipBlock();
    }
}
- (void)wdjMainUpdataVip{
    NSTimeInterval wdjVipBuyTime = (NSTimeInterval)[[NSUserDefaults standardUserDefaults] doubleForKey:@"wdjVipBuyTime"];
    NSTimeInterval wdjCurrentTime = [[NSDate date] timeIntervalSince1970];
    if(!wdjVipBuyTime||(wdjVipBuyTime <= wdjCurrentTime)){
        self.mainUserTitleLab.text = @"开通会员";
        self.mainUserVipLab.text = @"享受所有会员权益  体验翻倍";
        self.mainUserOpenVipBtn.hidden = false;
    }else{
        self.mainUserTitleLab.text = @"你已是会员";
        self.mainUserOpenVipBtn.hidden = true;
        
        NSDate *vipDate = [NSDate dateWithTimeIntervalSince1970:wdjVipBuyTime];
        // 创建日期格式化器
        NSDateFormatter *vipDateFormat = [[NSDateFormatter alloc] init];
        [vipDateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 设置日期格式
        // 格式化日期并输出
        NSString *curTime = [vipDateFormat stringFromDate:vipDate];
        self.mainUserVipLab.text = [NSString stringWithFormat:@"会员有效期:%@",curTime];
    }
}
@end
